<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Alterar senha</title>
    <link rel="stylesheet" href="./css/swal.css">
    <style>
        body {
            margin: 0;
            padding: 0;
            font-family: 'Microsoft YaHei', sans-serif;
            background-size: cover;
            background-position: center;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .container {
            background-color: rgba(255, 255, 255, 0.8);
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            text-align: center;
            position: fixed;
            top: 25%; /* 或其他你想要的固定位置 */
        }

        h2 {
            color: #c71585;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            font-size: 18px;
            display: block;
            margin-bottom: 8px;
            color: #333;
        }

        .form-group input {
            width: 100%;
            padding: 10px;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-sizing: border-box;
        }

        .form-group button {
            background-color: #c71585;
            color: #fff;
            padding: 12px 20px;
            font-size: 16px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        .back-to-login {
            margin-top: 10px;
            cursor: pointer;
            color: #333;
            text-decoration: underline;
        }
        button{
            margin-top: 10px;
            margin-right: 5px;
        }
        .button-group button {
            background-color: #c71585;
            color: #fff;
            padding: 12px 20px;
            font-size: 16px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            outline: none; /* 添加这一行来消除蓝色的外边框 */
            text-decoration: none; /* 添加这一行来去除下划线 */
        }

        .button-group button:hover {
            background-color: #a70e6e; /* 修改鼠标悬停时的背景颜色 */
        }

        .form-switch {
            display: flex;
            align-items: center;
            justify-content: center;
            margin-top: 20px;
        }

        .form-switch label {
            margin-right: 10px;
            font-size: 16px;
            color: #333;
            cursor: pointer; /* 为了提供一致的交互性 */
        }

        .form-switch input {
            display: none; /* 隐藏原始的复选框 */
        }
    </style>
</head>
<body>
<div class="container">
    <h2>Alterar sua senha</h2>
    <form id="changePasswordForm" class="form-group">
        <label for="currentPassword" style="margin-top: 5px">Senha atual：</label>
        <input type="password" id="currentPassword" name="currentPassword" required>
        <label for="newPassword" style="margin-top: 5px">Novas senhas：</label>
        <input type="password" id="newPassword" name="newPassword" required>
        <label for="confirmNewPassword" style="margin-top: 5px">Confirme sua nova senha：</label>
        <input type="password" id="confirmNewPassword" name="confirmNewPassword" required>
        <button type="submit" onclick="changePassword(event)">Confirme as alterações</button>
<!--        <button type="button" id="backToLogin" onclick="history.back()">返回</button>-->
    </form>
    <div class="form-switch" onclick="backToMine()">
        <label for="switch" id="switchLabel">retornar</label>
        <input type="checkbox" id="switch" name="switch">
    </div>
</div>
<script src="./js/swal.js"></script>
<script>
    function changePassword(event) {
        event.preventDefault();

        var currentPassword = document.getElementById("currentPassword").value;
        var newPassword = document.getElementById("newPassword").value;
        var confirmNewPassword = document.getElementById("confirmNewPassword").value;
        var username = localStorage.getItem("username");

        if (currentPassword === "" || newPassword === "" || confirmNewPassword === ""){
            Swal.fire({
                title: 'A modificação falhou',
                text: 'A senha atual, nova ou confirmada não pode estar vazia!',
                icon: 'error',
                confirmButtonText: 'OK'
            })
            return;
        }


        if (!username){
            Swal.fire({
                title: 'rápido',
                text: 'Faça login novamente！',
                icon: 'error',
                confirmButtonText: 'OK'
            }).then((result) => {
                // result.value 是用户点击确认按钮后的返回值
                if (result.value) {
                    // 这里可以根据具体情况进行其他操作，例如跳转页面等
                    window.location.href = "main.html";
                }
            });
            // alert("Faça login novamente！")

            return;
        }

        if (newPassword !== confirmNewPassword) {
            Swal.fire({
                title: 'rápido',
                text: 'A nova senha e a nova senha confirmada são inconsistentes, digite-a novamente！',
                icon: 'error',
                confirmButtonText: 'OK'
            });
            // alert("Faça login novamente！")
            // window.location.href = "main.html";
            // alert("A nova senha e a nova senha confirmada são inconsistentes, digite-a novamente.");
            return;
        }


        // 构建请求数据
        var changePasswordDto = {
            username:username,
            password: currentPassword,
            newPassword: newPassword
        };

        // 发送修改密码请求
        fetch("http://software.free.idcfengye.com/loginUser/changePassword", {
            method: "PUT",
            headers: {
                "Content-Type": "application/json"
            },
            body: JSON.stringify(changePasswordDto)
        })
            .then(response => response.json())
            .then(data => {
                console.log(data);
                // 处理修改密码成功或失败的逻辑
                handleResponse(data);
            })
            .catch(error => {
                Swal.fire({
                    title: 'Change Password Error',
                    text: 'Falha ao alterar a senha, tente novamente mais tarde！',
                    icon: 'error',
                    confirmButtonText: 'OK'
                });
                console.error("Change Password Error:", error);
                // alert("Falha ao alterar a senha, tente novamente mais tarde.");
            });
    }

    function backToLogin() {
        window.location.href = "main.html"; // 修改为你的登录页面路径
    }

    function backToMine(){
        window.location.href="mine.html";
    }

    function handleResponse(response) {
        if (response.success) {
            // 操作成功
            Swal.fire({
                title: 'rápido',
                text: 'A senha foi alterada com êxito！',
                icon: 'error',
                confirmButtonText: 'OK'
            }).then((result) => {
                // result.value 是用户点击确认按钮后的返回值
                if (result.value) {
                    // 这里可以根据具体情况进行其他操作，例如跳转页面等
                    backToLogin();
                }
            });
            // alert("A senha foi alterada com êxito：" + response.message);

        } else {
            // 操作失败
            Swal.fire({
                title: 'rápido',
                text: 'Falha ao alterar a senha！',
                icon: 'error',
                confirmButtonText: 'OK'
            });
            // alert("Falha ao alterar a senha：" + response.message);
        }
    }
</script>
</body>
</html>
